﻿Imports ATS.ValidateKeys
Namespace LightSwitchApplication
    Public Class jokoatsDataService
        Dim isValidLicense As Boolean = True

        Private Sub MainAssetsTables_CanDelete(ByRef result As Boolean)
            If Not isValidLicense Then
                result = False
            Else
                result = Me.Application.User.HasPermission(Permissions.CanDeleteAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
            End If
        End Sub

        Private Sub MainAssetsTables_CanInsert(ByRef result As Boolean)
            If Not isValidLicense Then
                result = False
            Else
                result = Me.Application.User.HasPermission(Permissions.CanInsertAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
            End If

        End Sub

        Private Sub MainAssetsTables_CanUpdate(ByRef result As Boolean)
            If Not isValidLicense Then
                result = False
            Else
                result = Me.Application.User.HasPermission(Permissions.CanEditAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
            End If
        End Sub

        Private Sub MainAssetsTables_Updating(entity As MainAssetsTable)
            entity.LastModified = Now
            entity.LastModifiedBy = Application.User.FullName
        End Sub

        Public Sub RFIDScanHistories_Inserting(entity As RFIDScanHistory, insertValue As RFIDScanHistory)
            Dim newScan = RFIDScanHistories.AddNew()
            With newScan
                .RFIDNum = entity.RFIDNum
                .DateScanned = entity.DateScanned
            End With
        End Sub

        Private Sub MainAssetsTables_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanViewLeasedAsset) _
                Or Me.Application.User.HasPermission(Permissions.CanViewDisposedAsset) _
                Or Me.Application.User.HasPermission(Permissions.CanViewWarrantyDetails) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub LeasedAssets_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyLeasedAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub DisposedAssets_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanViewDisposedAsset) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub LeasedAssets_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanViewLeasedAsset) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub Warranties_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanViewWarrantyDetails) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub Categories_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanViewCategories) _
                Or Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub Categories_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageCategories) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Categories_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageCategories) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Categories_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageCategories) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Departments_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanViewDepartments) _
                Or Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports) _
                Or Me.Application.User.HasPermission(Permissions.CanViewEmployees)
        End Sub

        Private Sub Departments_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageDepartments) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Departments_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageDepartments) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Departments_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageDepartments) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Persons_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanViewEmployees) _
                Or Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub Persons_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageEmployees) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Persons_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageEmployees) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Persons_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageEmployees) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Locations_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanViewLocations) _
                Or Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub Locations_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageLocations) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Locations_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageLocations) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Locations_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageLocations) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub AssetClasses_CanRead(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanViewAssetClass) _
                Or Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen) _
                Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                Or Me.Application.User.HasPermission(Permissions.CanRunReports) _
                Or Me.Application.User.HasPermission(Permissions.CanViewCategories)
        End Sub

        Private Sub AssetClasses_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageAssetClass) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub AssetClasses_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageAssetClass) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub AssetClasses_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanManageAssetClass) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub LeasedAssets_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyLeasedAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub LeasedAssets_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyLeasedAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub DisposedAssets_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyDisposedAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub DisposedAssets_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyDisposedAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub DisposedAssets_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyDisposedAsset) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Warranties_CanDelete(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyWarrantyDetails) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Warranties_CanInsert(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyWarrantyDetails) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub Warranties_CanUpdate(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.CanModifyWarrantyDetails) Or Me.Application.User.HasPermission(Permissions.ViewAdminScreen)
        End Sub

        Private Sub AssetsByCategory_CanExecute(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                              Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub AssetsByDeptName_CanExecute(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                              Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        Private Sub AssetsByLocation_CanExecute(ByRef result As Boolean)
            result = Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
                              Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        End Sub

        'Private Sub AssetByLastScanDate_CanExecute(ByRef result As Boolean)
        '    result = Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
        '                      Or Me.Application.User.HasPermission(Permissions.CanRunReports)
        'End Sub

        Private Sub AssetNotScanned_CanExecute(ByRef result As Boolean)
            'result = Me.Application.User.HasPermission(Permissions.ViewAdminScreen) _
            '                  Or Me.Application.User.HasPermission(Permissions.CanRunReports) _
            '                  Or Me.Application.User.HasPermission(Permissions.CanAccessInitialScreen)
        End Sub

#Region "Queries"
        Private Sub AssetsByDeptName_PreprocessQuery(p_departmentCode As String, ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.MainAssetsTable))
            If p_departmentCode IsNot Nothing Then
                query = query.Where(Function(c) c.Department.DepartmentName = p_departmentCode)
            End If
        End Sub

        Private Sub AssetsByCategory_PreprocessQuery(pCategoryName As String, ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.MainAssetsTable))
            If pCategoryName IsNot Nothing Then
                query = query.Where(Function(c) c.Category.CategoryName = pCategoryName)
            End If
        End Sub

        Private Sub AssetsByLocation_PreprocessQuery(p_Location As String, ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.MainAssetsTable))
            If p_Location IsNot Nothing Then
                query = query.Where(Function(l) l.Location.LocationName = p_Location)
            End If
        End Sub


        Private Sub ScanHistoryByAssetNumber_PreprocessQuery(pAssetNumber As String, ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.RFIDScanHistory))
            If pAssetNumber IsNot Nothing Then
                query = query.Where(Function(x) x.RFIDNum = pAssetNumber)
            End If
        End Sub
#End Region


        
    End Class

End Namespace
